package com.classbook.violet.baigiang6.subject;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;
 
/*
 * class StudyHandler *
 */
public class SubjectHandler extends DefaultHandler {
 
    // members
    private boolean isSubject = false;
    private boolean isLesson = false;
    private boolean isUnit= false;
    
    private LessonNode tempLesson;
    private List<LessonNode> lessonList;
    
    private UnitNode tempUnit;
    private List<UnitNode> unitList;
    
    private SubjectNode tempSubject;
    private List<SubjectNode> subjectList;
    private String[] list_lesson = {};
    
 
    // 'getter' is enough
    
    public List<LessonNode> getListSubject (){
    	return lessonList;
    }
    
    public List<UnitNode> getListLession (){
    	return unitList;
    }
    
    public List<SubjectNode> getListStudy (){
    	return subjectList;
    }
 
    /*
     * (non-Javadoc)
     * @see org.xml.sax.helpers.DefaultHandler#startDocument()
     */
    @Override
    public void startDocument() throws SAXException {
        // create new object
    	subjectList = new ArrayList<SubjectNode>();
    	lessonList = new ArrayList<LessonNode>();
    	unitList = new ArrayList<UnitNode>();
    }
 
    /*
     * (non-Javadoc)
     * @see org.xml.sax.helpers.DefaultHandler#endDocument()
     */
    @Override
    public void endDocument() throws SAXException {
        // nothing we need to do here
    }
 
    /*
     * (non-Javadoc)
     * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
     */
    @Override
    public void startElement(String namespaceURI, String localName, String qName, Attributes atts)
                            throws SAXException {
        // if this element value equals "study"
//    	Log.i("START ELEMENT", localName + "/" + qName + ":" + atts.getValue("label").toString());

        if(localName.equals("node")){
        	if (atts.getValue("label") != null){
//        	Log.i("CUT STRING", atts.getValue("label").toString());
        	if (atts.getValue("type").toString().contains("subject")){
        		isSubject = true;
        		tempSubject = new SubjectNode();
        		tempSubject.label = atts.getValue("label").toString();
        	}
        	else if (atts.getValue("type").toString().contains("unit")){
        		isUnit = true;
        		tempUnit = new UnitNode();
        		tempUnit.label = atts.getValue("label").toString();
        	}else if (atts.getValue("type").toString().contains("lesson")){
        		isLesson = true;
        		tempLesson = new LessonNode();
        		tempLesson.label = atts.getValue("label").toString();
        		String temp_utl = atts.getValue("url").toString();
        		String[] temp_split = temp_utl.split("/");
        		tempLesson.url = temp_split[temp_split.length - 1];
        	}
        	}
        }

    }
 
    /*
     * (non-Javadoc)
     * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
     */
    @Override
    public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
    	 
//    	Log.i("End Element", localName + "/" + qName+ ":" + atts.getValue("label").toString());
    	
    	if(localName.equals("node")){
    		if (isLesson){
    			tempUnit.lessonList.add(tempLesson);
    			isLesson = false;
    		}else if (isUnit){
    			tempSubject.unitList.add(tempUnit);
    			isUnit = false;
    		}else if (isSubject){
    			subjectList.add(tempSubject);
    			isSubject = false;
    		}
    	}

    }
 
    /*
     * (non-Javadoc)
     * @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
     */
    @Override
    public void characters(char ch[], int start, int length) {
        // get all text value inside the element tag
        String chars = new String(ch, start, length);
        chars = chars.trim(); // remove all white-space characters

    }
}
